﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using RemoteScriptingLibrary;
using Win32 = RemoteScriptingLibrary.Microsoft.Win32.Win32;

namespace WindowsRemoteScripting
{
    public class WindowInfo
    {
        protected string m_sCaption = string.Empty;
        protected string m_sClassName = string.Empty;

        protected string m_sModuleFileName = string.Empty;
        protected IntPtr m_Handle = IntPtr.Zero;
        protected string m_sText = string.Empty;

        public WindowInfo()
        {
        }

        public void RetrieveInfo(IntPtr ptr)
        {
            m_Handle = ptr;
            m_sCaption = Helper.GetControlText(ptr);

            StringBuilder sbClassName = new StringBuilder(255);
            Win32.GetClassName(ptr, sbClassName, 255);
            string sClassName = sbClassName.ToString();

            int hInstance = -1;
            hInstance = Win32.GetWindowLong(ptr, Win32.GWW_HINSTANCE);

            StringBuilder sbModuleFileName = new StringBuilder(255);
            int r = Win32.GetModuleFileName(new IntPtr(hInstance), sbModuleFileName, 100);
            m_sModuleFileName = sbModuleFileName.ToString();
            m_sClassName = sClassName;

            if (m_sClassName.ToLower().Contains("listbox"))
            {
                m_sText = string.Join(Environment.NewLine, Helper.GetListBoxText(ptr));
            }
            else
            {
                m_sText = Helper.GetWindowTextRaw(ptr);
            }

        }

        public string Caption
        {
            get { return m_sCaption; }
        }

        public string ClassName
        {
            get { return m_sClassName; }
        }

        public string ModuleFilename
        {
            get { return m_sModuleFileName; }
        }

        public IntPtr Handle
        {
            get { return m_Handle; }
        }

        public string Text
        {
            get { return m_sText; }
        }
    }
}
